package ke24.ga.extension;

import java.util.HashMap;

import org.jgap.Gene;

import ke24.ga.Package;

import com.softhinker.ke24.unit2.data.DataRetriever;

public class SolutionDataRetriever implements DataRetriever{

    private Package[] packages;
    private Gene[] genes;
    private HashMap<Integer, Double> weightMap;

    public SolutionDataRetriever(Package[] packages, Gene[] genes, HashMap<Integer,Double> weightMap) {
        this.packages = packages;
        this.genes = genes;
        this.weightMap = weightMap;
    }
    public String[] getColumnNames() {
        String[] columns = {"Position","Type", "Package ID","Weight",  "Group Weight"};
        return columns;
    }

    public Package[] getPackages() {
        return this.packages;
    }

    public Object[][] retrieveData() {        
        Object[][] data = new Object[this.genes.length][5];
        double groupWeight = 0;
        for(int i = 0; i < this.genes.length; i++)
        {
            if (i%4 == 0) groupWeight = 0;              
            int pID = ((Integer)genes[i].getAllele()).intValue();
            double weight = this.weightMap.get((Integer)genes[i].getAllele());    
            groupWeight += weight;
            data[i][0] = getLocation(i);
            data[i][1] = Package.isBig(pID)?"Big":"Small";
            data[i][2] = pID;
            data[i][3] = weight;
            data[i][4] = groupWeight;
            if (i%4 == 3) {
                data[i-1][4] = data[i-2][4] = data[i-3][4] = data[i][4];
            }
        }
        return data;
    }
    
    private String getLocation(int i) {
        int group = i/4 + 1;
        int level = i % 4 + 1;
        return group + "" + level;        
    }
    
}
